package com.government.sequence;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.RandomUtil;
import com.government.common.enums.PayTypeEnum;
import com.government.common.utils.StringUtil;
import com.government.sequence.key.SequenceKeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;

import static com.government.common.utils.StringUtil.ZERO_FORMAT_JOURNEY_ME_ORDER_NO;


@Service
public class SequenceService {


    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 根据支付类型获得支付流水号
     * format:  XX + YYYYMMDD+ 9位随机数字（自增的值随机）
     * eg:  BX2019090509452312354321
     *
     * @param payType 支付类型
     * @return 返回订单编号
     */
    public String getOutTradeNo(PayTypeEnum payType) {

        Objects.requireNonNull(payType, "支付类型不能为空");

        // yyyyMMddHHmmssSSS
        final String currentTimeStr = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN);

        SequenceKeyGenerator generator = SequenceKeyGenerator.OUT_TRADE_NO().buildKey(DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN));

        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        operations.setIfAbsent(generator.getKey(), BigDecimal.ZERO.toString(), generator.expireSeconds(), TimeUnit.SECONDS);

        // 获得自增序列, 序列号自增的值随机
        Long incr = operations.increment(generator.getKey(), RandomUtil.randomInt(1, 100));

        StringJoiner stringJoiner = new StringJoiner(StringUtil.EMPTY_STRING);
        stringJoiner.add(payType.getPrefix());
        stringJoiner.add(currentTimeStr);
        stringJoiner.add(StringUtil.formatToSequence(incr, ZERO_FORMAT_JOURNEY_ME_ORDER_NO));

        return stringJoiner.toString();
    }

    /**
     * 生成订单号
     * format:  XX + YYYYMMDD+ 9位随机数字（自增的值随机）
     * eg:  BX2019090509452312354321
     *
     * @return 返回订单编号
     */
    public String getOutTradeNo(String prefix) {

        // yyyyMMddHHmmssSSS
        final String currentTimeStr = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN);

        SequenceKeyGenerator generator = SequenceKeyGenerator.VIP_ORDER_NO().buildKey(DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN));

        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        operations.setIfAbsent(generator.getKey(), BigDecimal.ZERO.toString(), generator.expireSeconds(), TimeUnit.SECONDS);

        // 获得自增序列, 序列号自增的值随机
        Long incr = operations.increment(generator.getKey(), RandomUtil.randomInt(1, 100));

        StringJoiner stringJoiner = new StringJoiner(StringUtil.EMPTY_STRING);
        stringJoiner.add(prefix);
        stringJoiner.add(currentTimeStr);
        stringJoiner.add(StringUtil.formatToSequence(incr, ZERO_FORMAT_JOURNEY_ME_ORDER_NO));

        return stringJoiner.toString();
    }

}
